﻿Imports WiremetModel.Entities.SupplyOrders
Imports WiremetModel.Entities.OEM
Imports InputValidation
Imports InputValidation.GeneralRules
Imports InputValidation.GeneralRules.NumericInputRules
Imports System.ComponentModel

Public Class WireCertificateMechanicalParametersInputDialog

    Private _mechanicalParameters As WireCertificateMechanicalParameters
    Public Property MechanicalParameters() As WireCertificateMechanicalParameters
        Get
            Return _mechanicalParameters
        End Get
        Set(ByVal value As WireCertificateMechanicalParameters)
            _mechanicalParameters = value
        End Set
    End Property

    Private _inputValidator As InputValidator


    Public Sub New()
        InitializeComponent()
        ClearErrorLabels()
        _mechanicalParameters = New WireCertificateMechanicalParameters()
        _inputValidator = New InputValidator()
    End Sub

    Public Sub New(ByVal mechanicalParameters As WireCertificateMechanicalParameters)
        InitializeComponent()
        ClearErrorLabels()
        _inputValidator = New InputValidator()
        _mechanicalParameters = mechanicalParameters

        txt_Endurance.Text = _mechanicalParameters.Endurance.ToString()
        txt_Percent.Text = _mechanicalParameters.Percent.ToString()
        txt_Strength.Text = _mechanicalParameters.Strength.ToString()
        txt_WireId.Text = _mechanicalParameters.WireId
    End Sub


    Private Sub ClearErrorLabels()
        lbl_EnduranceError.Text = String.Empty
        lbl_PercentError.Text = String.Empty
        lbl_StrengthError.Text = String.Empty
        lbl_WireIdError.Text = String.Empty
    End Sub

#Region "Input validation"

    Private Sub txt_WireId_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_WireId.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_WireId, e, lbl_WireIdError, notNullable)
    End Sub

    Private Sub txt_WireId_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_WireId.Validated
        lbl_WireIdError.Text = String.Empty
    End Sub

    Private Sub txt_Strength_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_Strength.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numeric As New NumericInputValidatingSpecification()
        Dim numericPositive As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_Strength, e, lbl_StrengthError, notNullable.And(numeric).And(numericPositive))
    End Sub

    Private Sub txt_Strength_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_Strength.Validated
        lbl_StrengthError.Text = String.Empty
    End Sub


    Private Sub txt_Endurance_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_Endurance.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numeric As New NumericInputValidatingSpecification()
        Dim numericPositive As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_Endurance, e, lbl_EnduranceError, notNullable.And(numeric).And(numericPositive))
    End Sub

    Private Sub txt_Endurance_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_Endurance.Validated
        lbl_EnduranceError.Text = String.Empty
    End Sub


    Private Sub txt_Percent_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_Percent.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numeric As New NumericInputValidatingSpecification()
        Dim numericPositive As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_Percent, e, lbl_PercentError, notNullable.And(numeric).And(numericPositive))
    End Sub

    Private Sub txt_Percent_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_Percent.Validated
        lbl_PercentError.Text = String.Empty
    End Sub


#End Region

    

    Private Sub btn_Accept_Click(sender As System.Object, e As System.EventArgs) Handles btn_Accept.Click
        Try
            If Not ValidateChildren() Then
                Throw New EntityDataInputException("Podane dane są nieprawidłowe." & vbNewLine &
                                                   "Popraw je i spróbuj ponownie.")
            End If

            Dim confirmationDialogResult As DialogResult = MessageBox.Show("Czy jesteś pewień?", "Uwaga!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            If confirmationDialogResult = Windows.Forms.DialogResult.Yes Then

                With _mechanicalParameters
                    .Endurance = Decimal.Parse(txt_Endurance.Text.Trim())
                    .Percent = Decimal.Parse(txt_Percent.Text.Trim())
                    .Strength = Decimal.Parse(txt_Strength.Text.Trim())
                    .WireId = txt_WireId.Text.Trim()
                End With
                DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As EntityDataInputException
            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class